
var editor ;
jQuery(document).ready(function() {
	var id = jQuery("#articleId").val();
	if(typeof(id) !=undefined && id !=""){
		jQuery.ajax({
			  type: "GET",
			  dataType: 'json',
			  url: "/article/"+id,
			  success: function(data){
				  selectVal(data.type);
				  var text = data.text;
				  jQuery(".simditor-placeholder").html("");
				  jQuery(".simditor-body").html(text);
				  jQuery("#title").val(data.title);
				  alert(data.id);
				  jQuery("#articleId").val(data.id);
			  }
		});
	}else{
		jQuery("#articleId").val(0);
		selectVal2(0);
	}
	
	editor= new Simditor({
		  textarea: jQuery('#editor')
	});
});

function submit(){
	var text= editor.getValue();
	if(text==""){
		alert("写点什么吧");
		return;
	}
	var title = jQuery("#title").val();
	var cars = jQuery("#cars").val();
	var id = jQuery("#articleId").val();
	jQuery.ajax({
		  type: "POST",
		  data:{title:title,text:text,id:id,type:cars},
		  dataType: 'json',
		  url: "article/save",
		  success: function(data){
			  alert(data);
			  window.location.href ="pre";
		  }
	});
	
}
function ret(){
	var id = jQuery("#articleId").val();
	if(typeof(id) == undefined || id==""){		
		window.location.href =window.location.href+"../../../";
	}
	window.location.href =window.location.href+"../../../../";
}
/**
 * 有数据
 * @param flag 绑定selected
 */
function selectVal(flag){
	jQuery.ajax({
		  type: "POST",
		  dataType: 'json',
		  url: "../../type/list",
		  success: function(data){
		  	 var optionstring = "";  
           for (var j = 0; j < data.length; j++) {  
          	 if(flag==data[j].id){   
          		 optionstring += "<option value=\"" + data[j].id + "\" selected >" + data[j].name + "</option>";  
          	 }else{            		 
          		 optionstring += "<option value=\"" + data[j].id + "\" >" + data[j].name + "</option>";  
          	 }
           }  
		  	jQuery("#cars").html("<option value='0'>请选择...</option> "+optionstring);
		  }
	});
}
function selectVal2(flag){
	jQuery.ajax({
		type: "POST",
		dataType: 'json',
		url: "../type/list",
		success: function(data){
			var optionstring = "";  
			for (var j = 0; j < data.length; j++) {  
				if(flag==data[j].id){            		 
					optionstring += "<option value=\"" + data[j].id + "\" selected >" + data[j].name + "</option>";  
				}else{            		 
					optionstring += "<option value=\"" + data[j].id + "\" >" + data[j].name + "</option>";  
				}
			}  
			jQuery("#cars").html("<option value='0'>请选择...</option> "+optionstring);
		}
	});
}